home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / tcpdump-.001 / tcpdump-~ / tcpdump-3.0.2-linux / tcpdump-3.0.2 / print-tftp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-15  |  3.9 KB  |  146 lines

  1. /*
  2.  * Copyright (c) 1990, 1991, 1993, 1994
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  *
  21.  * Format and print trivial file transfer protocol packets.
  22.  */
  23.  
  24. #ifndef lint
  25. static char rcsid[] =
  26.     "@(#) $Header: print-tftp.c,v 1.20 94/06/14 20:18:49 leres Exp $ (LBL)";
  27. #endif
  28.  
  29. #include <sys/param.h>
  30. #include <sys/time.h>
  31. #include <sys/types.h>
  32.  
  33. #include <netinet/in.h>
  34.  
  35. #include <arpa/tftp.h>
  36.  
  37. #include <ctype.h>
  38. #include <stdio.h>
  39. #include <string.h>
  40.  
  41. #include "interface.h"
  42. #include "addrtoname.h"
  43.  
  44. /* op code to string mapping */
  45. static struct token op2str[] = {
  46.     { RRQ,        "RRQ" },    /* read request */
  47.     { WRQ,        "WRQ" },    /* write request */
  48.     { DATA,        "DATA" },    /* data packet */
  49.     { ACK,        "ACK" },    /* acknowledgement */
  50.     { ERROR,    "ERROR" },    /* error code */
  51.     { 0,        NULL }
  52. };
  53.  
  54. /* error code to string mapping */
  55. static struct token err2str[] = {
  56.     { EUNDEF,    "EUNDEF" },    /* not defined */
  57.     { ENOTFOUND,    "ENOTFOUND" },    /* file not found */
  58.     { EACCESS,    "EACCESS" },    /* access violation */
  59.     { ENOSPACE,    "ENOSPACE" },    /* disk full or allocation exceeded */
  60.     { EBADOP,    "EBADOP" },    /* illegal TFTP operation */
  61.     { EBADID,    "EBADID" },    /* unknown transfer ID */
  62.     { EEXISTS,    "EEXISTS" },    /* file already exists */
  63.     { ENOUSER,    "ENOUSER" },    /* no such user */
  64.     { 0,        NULL }
  65. };
  66.  
  67. /*
  68.  * Print trivial file transfer program requests
  69.  */
  70. void
  71. tftp_print(register const u_char *bp, int length)
  72. {
  73.     register const struct tftphdr *tp;
  74.     register const char *cp;
  75.     register const u_char *ep, *p;
  76.     register int opcode;
  77. #define TCHECK(var, l) if ((u_char *)&(var) > ep - l) goto trunc
  78.     static char tstr[] = " [|tftp]";
  79.  
  80.     tp = (const struct tftphdr *)bp;
  81.     /* 'ep' points to the end of avaible data. */
  82.     ep = snapend;
  83.  
  84.     /* Print length */
  85.     printf(" %d", length);
  86.  
  87.     /* Print tftp request type */
  88.     TCHECK(tp->th_opcode, sizeof(tp->th_opcode));
  89.     opcode = ntohs(tp->th_opcode);
  90.     cp = tok2str(op2str, "tftp-#%d", opcode);
  91.     printf(" %s", cp);
  92.     /* Bail if bogus opcode */
  93.     if (*cp == 't')
  94.         return;
  95.  
  96.     switch (opcode) {
  97.  
  98.     case RRQ:
  99.     case WRQ:
  100.         putchar(' ');
  101.         /*
  102.          * XXX Not all arpa/tftp.h's specify th_stuff as any
  103.          * array; use address of th_block instead
  104.          */
  105. #ifdef notdef
  106.         p = (u_char *)tp->th_stuff;
  107. #else
  108.         p = (u_char *)&tp->th_block;
  109. #endif
  110.         if (fn_print(p, ep)) {
  111.             fputs(&tstr[1], stdout);
  112.             return;
  113.         }
  114.         break;
  115.  
  116.     case DATA:
  117.         TCHECK(tp->th_block, sizeof(tp->th_block));
  118.         printf(" block %d", ntohs(tp->th_block));
  119.         break;
  120.  
  121.     case ACK:
  122.         break;
  123.  
  124.     case ERROR:
  125.         /* Print error code string */
  126.         TCHECK(tp->th_code, sizeof(tp->th_code));
  127.         printf(" %s ", tok2str(err2str, "tftp-err-#%d",
  128.                        ntohs(tp->th_code)));
  129.         /* Print error message string */
  130.         if (fn_print((const u_char *)tp->th_data, ep)) {
  131.             fputs(&tstr[1], stdout);
  132.             return;
  133.         }
  134.         break;
  135.  
  136.     default:
  137.         /* We shouldn't get here */
  138.         printf("(unknown #%d)", opcode);
  139.         break;
  140.     }
  141.     return;
  142. trunc:
  143.     fputs(tstr, stdout);
  144. #undef TCHECK
  145. }
  146.